using System;
using System.Diagnostics;
using log4net;

namespace OglivyOne.Ext.Log
{
    /// <summary>
    /// A wrapper of ILogger using log4net
    /// </summary>
    [DebuggerNonUserCode]
    public class Log4net : ILogger
    {
        #region -- Private Fields --
        private readonly ILog _logger;
        private bool _isDebugEnable;
        private bool _isErrorEnable;
        #endregion

        #region -- Constructor --
        /// <summary>
        /// Initializes a new instance of the <see cref="Log4net"/> class.
        /// </summary>
        public Log4net()
        {
            _logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        }
        #endregion

        #region -- ILogger Members --
        /// <summary>
        /// Infoes the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Info(string message)
        {
            _logger.Info(message);
        }

        /// <summary>
        /// Warns the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Warn(string message)
        {
            _logger.Warn(message);
        }

        /// <summary>
        /// Debugs the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Debug(string message)
        {
            if (_isDebugEnable)
            {
                _logger.Debug(message);
            }
        }

        /// <summary>
        /// Errors the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Error(string message)
        {
            if (_isErrorEnable)
            {
                _logger.Error(message);
            }
        }
        /// <summary>
        /// Errors the specified x.
        /// </summary>
        /// <param name="x">The x.</param>
        public void Error(Exception x)
        {
            if (_isErrorEnable)
            {
                _logger.Error(x.Message, x);
            }
        }
        /// <summary>
        /// Fatals the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Fatal(string message)
        {
            _logger.Fatal(message);
        }
        /// <summary>
        /// Fatals the specified x.
        /// </summary>
        /// <param name="x">The x.</param>
        public void Fatal(Exception x)
        {
            _logger.Fatal(x.Message, x);
        }

        /// <summary>
        /// Configs the log.
        /// </summary>
        public void ConfigLog()
        {
            log4net.Config.XmlConfigurator.Configure();
            _isDebugEnable = _logger.IsDebugEnabled;
            if (_isDebugEnable)
            {
                _logger.Info("Logging DEBUG is enable");
            }
            else
            {
                _logger.Info("Logging DEBUG is disable!!!");
            }
            _isErrorEnable = _logger.IsErrorEnabled;
        }

        /// <summary>
        /// Errors the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="x">The x.</param>
        public void Error(string message, Exception x)
        {
            _logger.Error(message, x);
        }

        #endregion
    }
}
